package com.aluka.nirvana.framework.datasource.configuration;

import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import javax.sql.DataSource;

/**
 * 自定义Jpa实体管理类
 * 创建一个适合于仅使用 JPA 进行数据访问的环境的 EntityManager，
 * 构建的时候需要指明提示实体类的包路径、数据源和 JPA 配置信息
 * @author gongli
 * @since 2019/12/18
 */
public class JpaEntityManager extends LocalContainerEntityManagerFactoryBean {

    public JpaEntityManager(DataSource dataSource,
                            String packagesToScan,
                            String persistenceUnitName,
                            JpaVendorAdapter jpaVendorAdapter,
                            JpaProperties jpaProperties){
        setDataSource(dataSource);
        setPackagesToScan(packagesToScan.split(","));
        setPersistenceUnitName(persistenceUnitName);
        setJpaVendorAdapter(jpaVendorAdapter);
        setJpaPropertyMap(jpaProperties.getProperties());
    }
}
